#include "CGlobal.h"
#include "CVMDataTypes.h"
#include "data/CFunction.h"
#include "functions/CIO.h"

QTextStream& CGlobal::output = createOutput();

QTextStream& CGlobal::createOutput()
{
    static QTextStream rslt(stdout);
    rslt.setCodec("IBM866");
    return rslt;
}

QHash<QString, char*>&CGlobal::globalVariables = createGlobalVariables();

QHash<QString, char*>& CGlobal::createGlobalVariables()
{
    static QHash<QString, char*> rslt;

    createFunctionPrint(rslt);
    createFunctionPrintln(rslt);

    return rslt;
}


void CGlobal::createFunctionPrint(QHash<QString, char*>& container)
{
    QVector<QString>* prmtrs = new QVector<QString>();
    prmtrs->append(CPrint::parameter1);

    QString nm = CPrint::name;

    char* fnctn = new char[sizeof(CFunction) + 1];
    *fnctn = (char)CVMDataTypes::FUNCTION;
    new((void*)(fnctn + 1)) CFunction(CPrint::print, CPrint::parameterCount, QHash<QString, char*>(), prmtrs);
    container[nm] = fnctn;
}

void CGlobal::createFunctionPrintln(QHash<QString, char*>& container)
{
    QVector<QString>* prmtrs = new QVector<QString>();
    prmtrs->append(CPrintln::parameter1);

    QString nm = CPrintln::name;

    char* fnctn = new char[sizeof(CFunction) + 1];
    *fnctn = (char)CVMDataTypes::FUNCTION;
    new((void*)(fnctn + 1)) CFunction(CPrintln::println, CPrint::parameterCount, QHash<QString, char*>(), prmtrs);
    container[nm] = fnctn;
}
